package com.wpp.algorithm.lagou.part1.lesson1;

import java.util.Arrays;

/**
 * @description:
 * 链表 leetcode2  ：https://leetcode-cn.com/problems/add-two-numbers/ 的变形
 *  给出两个非空链表用来表示两个非负整数
 *  其中它们各自的位数是按照逆序的方式存储的，并且它们的每个节点只能存储一位数字
 *
 *  如果我们将这两个数相加起来，则会返回一个新的链表来表示它们的和
 *       您可以假设除了数字 0 之外，这两个数都不会以 0 开头
 *       输入：(2 -> 4 -> 3) + (5 -> 6 -> 4)
 *       输出：7 -> 0 -> 8
 *       原因：342 + 465 = 807
 * @author: WangPengPeng
 * @create: 2022-01-23 14:17
 **/
public class Part1_2 {
    public static void main(String[] args) {
        MyNode node = new MyNode(new MyNode(new MyNode(null, 3), 4), 2);
        MyNode node2 = new MyNode(new MyNode(new MyNode(null, 4), 6), 5);
        System.out.println(fun(node,node2));

        Arrays.stream(fun(new int[]{1, 3, 5, 7, 9}, 12)).forEach(System.out::println);
    }
    /**
     *
     * @param myNode1
     * @param myNode2
     * @return
     */
    private static int fun(MyNode myNode1,MyNode myNode2) {
        MyNode node1 = myNode1;
        MyNode node2 = myNode2;
        StringBuilder sb = new StringBuilder();
        while (node1 != null) {
            sb.append(node1.value);
            node1 = node1.next;
        }
        StringBuilder sb2 = new StringBuilder();
        while (node2 != null) {
            sb2.append(node2.value);
            node2 = node2.next;
        }
        long l = Long.parseLong(sb.toString());
        long l2 = Long.parseLong(sb2.toString());

        return (int) (l + l2);
    }

    /**
     * 链表 leetcode2  ：https://leetcode-cn.com/problems/add-two-numbers/
     */
    private static int[] fun (int[] arr ,int target) {
        int beginIndex = 0;
        int secondIndex = 1;
        int length = arr.length;
        while (target - arr[secondIndex] != arr[beginIndex]) {
            if (secondIndex != length -1) {
                secondIndex++;
                continue;
            }
            beginIndex++;
            secondIndex = beginIndex + 1;
        }
        return new int[]{beginIndex,secondIndex};
    }
}


class MyNode {

    public MyNode next;
    public int value;

    public MyNode(MyNode next, int value) {
        this.next = next;
        this.value = value;
    }
}
